package cn.yunyichina.provider.message.utils;

import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

/**
 * Created by Joe.zhou on 2017/9/12.
 */
public class MsgTemplateUtils {

    private static Logger logger = LoggerFactory.getLogger(MsgTemplateUtils.class);

    /**
     * 输出骨科6期选择相同科室
     * @param inputJson
     * @param dbJson
     * @return
     */
    public static String deptDifferDatas(String inputJson , String dbJson){
        Map<String,String> inputMap = JsonUtils.parseObject(inputJson, Map.class);
        Map<String,String> dbMap = JsonUtils.parseObject(dbJson, Map.class);

        Map<String,String> resultMap = new HashMap<String,String>();

        Iterator<String> iterator = dbMap.keySet().iterator();
        while (iterator.hasNext()){
            String dbKey = iterator.next();
            if(inputMap.containsKey(dbKey)){
                String[] dbArray = dbMap.get(dbKey).split(";");
                String[] inputArray = inputMap.get(dbKey).split(";");

                try {
                    String[] sameArray = intersect(dbArray, inputArray);
                    if(sameArray.length > 0){
                        resultMap.put(dbKey , JsonUtils.toJsonString(sameArray));
                    }
                } catch (Exception e) {
                    logger.error("[MsgTemplateUtils.deptDifferDatas] 判断科室相同工具报错");
                    e.printStackTrace();
                }
            }
        }

        String resultStr = null;

        if(resultMap.size() > 0){
            resultStr = JsonUtils.toJsonString(resultMap);
        }

        logger.info("MsgTemplateUtils.deptDifferDatas 得出相同科室：" + resultStr);

        return resultStr;
    }




    /**
     * 求两个字符串数组的交集
     * @param strArray1 字符串数组1
     * @param strArray2 字符串数组2
     * @return 两个字符串数组的交集
     */
    private static String[] intersect(String[] strArray1, String[] strArray2) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        LinkedList<String> list = new LinkedList<String>();
        // 通过Map来获取两个集合的相同元素
        for (String str : strArray1) {
            if (!map.containsKey(str)) {
                map.put(str, Boolean.FALSE);
            }
        }
        for (String str : strArray2) {
            if (map.containsKey(str)) {
                map.put(str, Boolean.TRUE);
            }
        }
        // 将Map中相同元素添加加list中
        for (Map.Entry<String, Boolean> e : map.entrySet()) {
            if (e.getValue().equals(Boolean.TRUE)) {
                list.add(e.getKey());
            }
        }
        String[] result = new String[0];
        return list.toArray(result);
    }




    public static void main(String[] args) {
        String[] a = {"1","2","3"};
        String[] b = {"4","5","6"};

        String[] strings = intersect(a, b);

        System.out.println(JsonUtils.toJsonString(strings));
        if(strings.length < 1){
            System.out.println("xxxxxxxx");
        }
    }
}
